define(['app'], function(app) {
    app.controller('CategoryController', ['$routeParams', 'Dialog', 'Table', 'CategoryService',
        function($routeParams, Dialog, Table, CategoryService) {
        var self = this;

        self.categoryId = $routeParams.id;
        self.categoryName = '';

        self.init = function() {
            // init
        };

        self.tableParams = Table.$create({
            showPage: false,
            group: {
                parentId: 'asc'
            }
        }, {
            groupOptions: {
                isExpanded: false
            },
            getData: function(params) {
                return CategoryService.getCategory({
                    id: self.categoryId,
                    name: self.categoryName,
                    orderBy: params.orderBy()
                }).then(function(resp) {
                    return resp.data;
                });
            }
        });

        self.reset = function() {
            self.categoryName = '';
        };

        self.search = function(init) {
            if (init) {
                self.categoryId = null;
                self.tableParams.page(1);
            }
            self.tableParams.reload(true);
        };

        self.editParent = function(category) {
            var data = {};
            if (category) {
                if (category.parentId == category.id) {
                    data.id = category.id;
                    data.name = category.name;
                } else {
                    data.id = category.parentId;
                    data.name = category.parentName;
                }
                data.parentId = data.id;
            }
            edit(data);
        };

        self.editSub = function(category, editable) {
            var data = {};
            if (category.parentId == category.id) {
                data.parentId = category.id;
            } else {
                data.parentId = category.parentId;
            }
            if (editable) {
                data.id = category.id;
                data.name = category.name;
            }
            edit(data);
        };

        self.removeParent = function(category) {
            var categoryIds = [];
            if (category.parentId == category.id) {
                categoryIds.push(category.id);
            } else {
                categoryIds.push(category.parentId);
            }
            remove(categoryIds);
        }

        self.removeSub = function(category) {
            remove([category.id]);
        }

        self.removeAll = function(category) {
            remove();
        }

        function edit(category) {
            Dialog.$open().editCategory({
                data: category
            }).then(function(data) {
                if (data.success) {
                    if (category) {
                        self.search();
                    } else {
                        self.search(true);
                    }
                }
            });
        }

        function remove(categoryIds) {
            if (!categoryIds) {
                categoryIds = [];
                angular.forEach(self.tableParams.select(), function(item) {
                    categoryIds.push(item.id);
                });
            }

            if (categoryIds.length == 0) {
                Dialog.$alert('请选择需要删除的财物分类！');
                return;
            }

            Dialog.$confirm('确定要删除此财物分类吗？').then(function() {
                CategoryService.removeCategory(categoryIds).then(function() {
                    self.search();
                });
            });
        }
    }]);
});
